package net.modul.dept.dao.impl;

import java.util.ArrayList;
import java.util.List;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;
import net.modul.dept.dao.DeptDao;
import net.modul.dept.pojo.Dept;
import net.modul.dept.vo.DeptVo;

public class DeptDaoImpl extends GeneralDaoImpl implements DeptDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<DeptVo> search(PageBean pageBean) throws Exception {
		StringBuilder hql = new StringBuilder("from Dept where flag=0");
		List<DeptVo> d = new ArrayList<DeptVo>();
		List<Dept> l = hibernateHandler.query(hql, pageBean);
		for(Dept dept : l) {
			DeptVo dv = new DeptVo();
			BeanUtils.copyProperties(dept, dv);
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insert(DeptVo bean) throws Exception {
		Dept dept = new Dept();
		BeanUtils.copyProperties(bean, dept);
		hibernateHandler.insert(dept);
	}

	@Override
	public void update(DeptVo bean) throws Exception {
		Dept dept = (Dept)hibernateHandler.find(Dept.class, bean.getDept_id());
		BeanUtils.copyProperties(bean, dept);
		hibernateHandler.update(dept);
	}

	@Override
	public void updateFlag(DeptVo bean) throws Exception {
		update(bean);
	}

	@Override
	public DeptVo find(Integer id) throws Exception {
		Dept dept = (Dept)hibernateHandler.find(Dept.class, id);
		DeptVo dv = new DeptVo();
		BeanUtils.copyProperties(dept, dv);
		return dv;
	}
}
